﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ZhCun.Framework.Common.Models.WhereHelperModel
{
    public abstract class WhereHelper
    {       
        #region 构造函数

        protected WhereHelper()
        {
            _WhereText = new StringBuilder();
            _ParamDict = new Dictionary<string, object>();
        }
        protected WhereHelper(string paramNameKey)
            : this()
        {
            _ParamNameKey = paramNameKey;
        }
        protected WhereHelper(WhereHelper wh)
            : this()
        {
            this._LastParamIndex = wh._LastParamIndex;
            this._ParamDict = wh._ParamDict;
            this._WhereText = wh._WhereText;
            this._ParamNameKey = wh._ParamNameKey;
        }

        #endregion

        #region 重写ToString

        public override string ToString()
        {
            return _WhereText.ToString();
        }

        #endregion

        #region 字段

        string _ParamNameKey;
        StringBuilder _WhereText;
        Dictionary<string, object> _ParamDict;
        int _LastParamIndex = -1;

        #endregion

        #region 属性

        /// <summary>
        /// 返回当前对象所有产品参数集合字典,key:参数数名(包含@),value:参数值
        /// </summary>
        public Dictionary<string, object> ParamDict
        {
            get
            {
                return _ParamDict;
            }
        }
        /// <summary>
        /// 返回当前生成where文本
        /// </summary>
        public StringBuilder WhereText
        {
            get
            {
                return _WhereText;
            }
        }

        #endregion

        #region 外部方法

        /// <summary>
        /// 增加查询参数,并输出参数名,参数名会根据LastParamIndex递增
        /// </summary>
        /// <param name="v">参数值</param>
        /// <returns>返回参数名</returns>
        protected string AddParam(object v)
        {
            _LastParamIndex++;
            string paramName = string.Format("@P_{1}{0}", _LastParamIndex, _ParamNameKey);
            _ParamDict.Add(paramName, v);
            return paramName;
        }
        /// <summary>
        /// 清除文本内容和参数内容
        /// </summary>
        public void Clear()
        {
            _ParamDict.Clear();
            _WhereText.Remove(0, _WhereText.Length);
        }
        /// <summary>
        /// 当前where文本增加字符(通常该值是 字段名 ),返回当前对象
        /// </summary>
        public virtual WhereHelper Add(string str)
        {
            this._WhereText.AppendFormat(" {0} ", str);
            return this;
        }
        /// <summary>
        /// 比较符,等于 "="
        /// </summary>
        public virtual WhereHelper Equal(object v)
        {
            this._WhereText.AppendFormat(" = {0}", AddParam(v));
            return this;
        }
        /// <summary>
        /// 比较符,不等于 "!="
        /// </summary>
        public virtual WhereHelper EqualNot(object v)
        {
            _WhereText.AppendFormat(" <> {0}", AddParam(v));
            return this;
        }
        /// <summary>
        /// 比较符,大于 ">"
        /// </summary>
        public virtual WhereHelper GreaterThan(object v)
        {
            this._WhereText.AppendFormat(" > {0}", AddParam(v));
            return this;
        }
        /// <summary>
        /// 比较符,小于
        /// </summary>
        public virtual WhereHelper LessThan(object v)
        {
            this._WhereText.AppendFormat(" < {0}", this.AddParam(v));
            return this;
        }
        /// <summary>
        /// 比较符,大于等于
        /// </summary>
        public virtual WhereHelper GreaterThenEqual(object v)
        {
            this._WhereText.AppendFormat(" >= {0}", this.AddParam(v));
            return this;
        }
        /// <summary>
        /// 比较符,小于等于
        /// </summary>
        public virtual WhereHelper LessThanEqual(object v)
        {
            this._WhereText.AppendFormat(" <= {0}", this.AddParam(v));
            return this;
        }
        /// <summary>
        /// 比较符,In,参数为某范围内的比较值
        /// </summary>
        public virtual WhereHelper In(params object[] inArgs)
        {
            this._WhereText.Append(" in (");
            for (int i = 0; i < inArgs.Length; i++)
            {
                _WhereText.Append(AddParam(inArgs[i]));
                if (i < inArgs.Length - 1)
                {
                    _WhereText.Append(",");
                }
            }
            this._WhereText.Append(" )");
            return this;
        }
        /// <summary>
        /// 比较符,NotIn
        /// </summary>
        public virtual WhereHelper NotIn(params object[] inArgs)
        {
            this._WhereText.Append(" not in (");
            for (int i = 0; i < inArgs.Length; i++)
            {
                _WhereText.Append(AddParam(inArgs[i]));
                if (i < inArgs.Length - 1)
                {
                    _WhereText.Append(",");
                }
            }
            this._WhereText.Append(" )");
            return this;
        }
        /// <summary>
        /// 比较符 left Like %{0}
        /// </summary>
        public virtual WhereHelper LikeLeft(string v)
        {
            string addParam = AddParam(string.Format("%{0}", v));
            this._WhereText.AppendFormat(" like {0}", addParam);
            return this;
        }
        /// <summary>
        /// 比较符 right like {0}%
        /// </summary>
        public virtual WhereHelper LikeRight(string v)
        {
            string addParam = AddParam(string.Format("{0}%", v));
            this._WhereText.AppendFormat(" like {0}", addParam);
            return this;
        }
        /// <summary>
        /// 比较符 full like
        /// </summary>
        public virtual WhereHelper LikeFull(string v)
        {
            string addParam = AddParam(string.Format("%{0}%", v));
            this._WhereText.AppendFormat(" like {0}", addParam);
            return this;
        }
        /// <summary>
        /// 增加一个左括号 "("
        /// </summary>
        public virtual WhereHelper BracketLeft()
        {
            this._WhereText.AppendFormat("(");
            return this;
        }
        /// <summary>
        /// 增加一个左括号"(",再加一个字符串(通常是一个字段)
        /// </summary>
        public virtual WhereHelper BracketLeft(string str)
        {
            this._WhereText.AppendFormat("(");
            this._WhereText.AppendFormat(str);
            return this;
        }
        /// <summary>
        /// 增加一个右括号 ")"
        /// </summary>
        public virtual WhereHelper BracketRight()
        {
            this._WhereText.Append(")");
            return this;
        }
        /// <summary>
        /// 增加连接符 "and"
        /// </summary>
        public virtual WhereHelper And()
        {
            And(string.Empty);
            return this;
        }
        /// <summary>
        /// 增加连接符 "and"
        /// </summary>
        public virtual WhereHelper And(string str)
        {
            if (this._WhereText.Length > 0)
            {
                this._WhereText.Append(" and ");
            }
            this._WhereText.Append(str);
            return this;
        }
        /// <summary>
        /// 增加连接符 "or"
        /// </summary>
        public virtual WhereHelper Or()
        {
            Or(string.Empty);
            return this;
        }
        /// <summary>
        /// 增加连接符"or",再加一个字符串(通常是一个字段)
        /// </summary>
        public virtual WhereHelper Or(string str)
        {
            if (this._WhereText.Length > 0)
            {
                this._WhereText.Append(" or ");
            }
            this._WhereText.Append(str);
            return this;
        }

        #endregion
    }
}
